<template>
  <ele-config-provider :map-key="MAP_KEY" :locale="eleLocale" :keep-alive="keepAlive" :license="LICENSE_CODE">
    <a-config-provider :locale="antLocale">
      <router-view />
    </a-config-provider>
  </ele-config-provider>
</template>

<script setup>
import { unref, computed, watch } from 'vue';
import { storeToRefs } from 'pinia';
import { useThemeStore } from '@/store/modules/theme';
import { MAP_KEY, LICENSE_CODE, TAB_KEEP_ALIVE } from '@/config/setting';
import { useSetDocumentTitle } from '@/utils/document-title-util';
import { useLocale } from '@/i18n/use-locale';
import { notification } from 'ant-design-vue';
import { SmileOutlined } from '@ant-design/icons-vue';
import { getRouteTabKey } from '@/utils/page-tab-util';

import { WEBSOCKET_URL } from '@/config/setting';

const themeStore = useThemeStore();
const { showTabs, tabs } = storeToRefs(themeStore);

// 恢复主题
themeStore.recoverTheme();

// 切换路由自动更新浏览器页签标题
useSetDocumentTitle();

// 国际化配置
const { antLocale, eleLocale } = useLocale();

// 用于内链 iframe 组件获取 KeepAlive
const keepAlive = computed(() => TAB_KEEP_ALIVE && unref(showTabs));

// const openNotification = () => {
//   notification.open({
//     message: '',
//     description:
//       'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
//     onClick: () => {
//       console.log('Notification Clicked!');
//     },
//     placement: 'bottomRight',
//     duration: 30,
//     maxCount: '2',
//   });
//   notification.open({
//     message: 'Notification Title',
//     description:
//       'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
//     onClick: () => {
//       console.log('Notification Clicked!');
//     },
//   });
// };
// document.addEventListener("click", openNotification)

//监听tab 销售单和入库单打开多页面时 title显示不对
// watch(() => tabs, (val) => {
//   if (!!sessionStorage) {
//     len = sessionStorage.length;
//   }
//   val.value.forEach(ele => {
//     console.log(ele.fullPath)
//   })
// },
//   { deep: true }
// )
// watch(() => getRouteTabKey(), (val) => {
//   console.log(val.value);
// },
//   { deep: true }
// )
document.onkeydown = function (evt) {
  evt = evt || window.event;
  var isEscape = false;
  if ("key" in evt) {
    isEscape = (evt.key === "Escape" || evt.key === "Esc");
  } else {
    isEscape = (evt.keyCode === 27);
  }
  if (isEscape) {
    notification.destroy()
  }
};

var socket;
if (typeof (WebSocket) == "undefined") {
  console.log("您的浏览器不支持WebSocket");
} else {
  console.log("您的浏览器支持WebSocket");
  //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
  socket = new WebSocket(WEBSOCKET_URL + "/webSocket/order".replace("http", "ws"));
  // socket = new WebSocket("ws://localhost:4337/webSocket/container".replace("http", "ws"));
  //打开事件
  socket.onopen = function () {
    //console.log("Socket 已打开");
    //socket.send("这是来自客户端的消息" + location.href + new Date());
  };
  //获得消息事件
  socket.onmessage = function (msg) {
    console.log(msg.data);
    notification.open({
      description: `${msg.data}`,
      placement: 'bottomRight',
      duration: 60,
    });
    //发现消息进入    开始处理前端触发逻辑
  };
  //关闭事件
  socket.onclose = function () {
    //console.log("Socket已关闭");
  };
  //发生了错误事件
  socket.onerror = function () {
    // console.log("Socket发生了错误");
    //此时可以尝试刷新页面
  }
}
</script>
